//
//  SLVerifyCodeVC.m
//  SlockApp
//
//  Created by 凯朱 on 2024/7/10.
//

#import "OSSendCodeVC.h"
@interface OSSendCodeVC ()<UITextFieldDelegate>

@end

@implementation OSSendCodeVC
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpSubViews];
}

-(void)setUpSubViews{
    _tipLbl2.text = kLocalString(@"请输入短信验证码");
    [_loginBtn setTitle:kLocalString(@"登录") forState:UIControlStateNormal];
    _tipLbl.text = [NSString stringWithFormat:@"%@%@",kLocalString(@"验证码已发送至："),_tel];
    _loginBtn.layer.cornerRadius = 25;
    _loginBtn.layer.masksToBounds = YES;
    self.view.backgroundColor = kColor_W;
    _tf.tintColor = [UIColor clearColor];
    _tf.textColor = kClearColor;
    [_tf addTarget:self action:@selector(textFieldChangeAction:) forControlEvents:UIControlEventEditingChanged];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self->_tf becomeFirstResponder];
    });
    _tf.keyboardType = UIKeyboardTypeNumberPad;
    _tf.delegate = self;
    [_loginBtn setBackgroundImage:[UIImage imageWithColor:kHexColor(@"#0E2673")] forState:UIControlStateNormal];
    [_loginBtn setBackgroundImage:[UIImage imageWithColor:kHexColor(@"#C6C6C6")] forState:UIControlStateDisabled];
    [_resendBtn startTimerCount:60];
    [self updateUI];
}

-(void)updateUI{
    _line1.backgroundColor = kHexColor(@"#DCDCDC");
    _line2.backgroundColor = kHexColor(@"#DCDCDC");
    _line3.backgroundColor = kHexColor(@"#DCDCDC");
    _line4.backgroundColor = kHexColor(@"#DCDCDC");
    _line5.backgroundColor = kHexColor(@"#DCDCDC");
    _line6.backgroundColor = kHexColor(@"#DCDCDC");
    _lbl1.text = _lbl2.text = _lbl3.text = _lbl4.text = _lbl5.text = _lbl6.text = @"";
    
    _loginBtn.enabled = _tf.text.length >= 6;
    
    //这里修改字符间距
    if (_tf.text.length >= 1) {
        NSString *str = [_tf.text substringWithRange:NSMakeRange(0, 1)];
        _lbl1.text = str;
        _line1.backgroundColor = kHexColor(@"#0A1B53");
    }
    if (_tf.text.length >= 2) {
        NSString *str = [_tf.text substringWithRange:NSMakeRange(1, 1)];
        _lbl2.text = str;
        _line2.backgroundColor = kHexColor(@"#0A1B53");
    }
    if (_tf.text.length >= 3) {
        NSString *str = [_tf.text substringWithRange:NSMakeRange(2, 1)];
        _lbl3.text = str;
        _line3.backgroundColor = kHexColor(@"#0A1B53");
    }
    if (_tf.text.length >= 4) {
        NSString *str = [_tf.text substringWithRange:NSMakeRange(3, 1)];
        _lbl4.text = str;
        _line4.backgroundColor = kHexColor(@"#0A1B53");
    }
    if (_tf.text.length >= 5) {
        NSString *str = [_tf.text substringWithRange:NSMakeRange(4, 1)];
        _lbl5.text = str;
        _line5.backgroundColor = kHexColor(@"#0A1B53");
    }
    if (_tf.text.length >= 6) {
        NSString *str = [_tf.text substringWithRange:NSMakeRange(5, 1)];
        _lbl6.text = str;
        _line6.backgroundColor = kHexColor(@"#0A1B53");
        
    }
}

-(void)textFieldChangeAction:(UITextField *)tf{
    if (tf.text.length > 6) {
        tf.text = [tf.text substringWithRange:NSMakeRange(0, 6)];
    }
    [self updateUI];
}

- (IBAction)sureAction:(id)sender {
    NSDictionary *params = @{@"phone":_tel,
                             @"captcha":_tf.text,
                             @"register_lat":@"",
                             @"register_lng":@"",
                             @"brand":@"",
                             @"rom":@"",
                             @"ram":@""};
    [OSTool showHudOnWindow];
    [OSHttpManager postWithPath:@"login/smsloginsaveT" params:params finished:^(BOOL isSuccessed, id result, NSString *code, NSString *message) {
        [OSTool hideHudOnKeyWindow];
        if (!isSuccessed) {
            kShowToast(message);
            return;
        }
        NSDictionary *data = result[@"data"];
        [[OSUserManager sharedOSUserManager] updateUserInfo:data];
        [self.navigationController popToRootViewControllerAnimated:YES];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"loginStatusChange" object:nil];
        OSTabBarController *tab = [OSTabBarController new];
        [UIApplication sharedApplication].keyWindow.rootViewController = tab;
    }];
}


- (IBAction)resendActiton:(id)sender {
    kWeakSelf(self);
    NSDictionary *params = @{@"phone":_tel};
    [OSTool showHudOnWindow];
    [OSHttpManager postWithPath:@"login/captcha" params:params finished:^(BOOL isSuccessed, id result, NSString *code, NSString *message) {
        [OSTool hideHudOnKeyWindow];
        kShowToast(message);
        if (!isSuccessed) {
            return;
        }
        [weakself.resendBtn startTimerCount:60];
    }];
}

@end
